﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Aspose.Cells;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;



public partial class Report_UserControl_uc_DoanhThu_Theo_KhuVuc : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();

        }
    }

    private void InitData()
    {

        GetNam();
        GetPhapNhan();
    }
    private void GetNam()
    {
        try
        {
            int Nam = DateTime.Now.Year;
            for (int i = Nam - 5; i < Nam + 5; i++)
            {
                ddlNam.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            ListItem lstParent = new ListItem("--Chọn--", "");
            ddlNam.Items.Insert(0, lstParent);
            ddlNam.SelectedIndex = ddlNam.Items.IndexOf(lstParent);
        }
        catch
        {
        }
    }
    private void GetPhapNhan()
    {
        try
        {
            CateBO cate = new CateBO();
            List<ZD_PRC_PHAPNHAN_GETBY_IDResult> lst = new List<ZD_PRC_PHAPNHAN_GETBY_IDResult>();
            lst = cate.GetPhapNhan("");
            if (lst != null)
            {
                ddlPhapNhan.DataSource = lst;
                ddlPhapNhan.DataTextField = "U_TenPN";
                ddlPhapNhan.DataValueField = "code";
                ddlPhapNhan.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "");
                ddlPhapNhan.Items.Insert(0, lstParent);
                ddlPhapNhan.SelectedIndex = ddlPhapNhan.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }
    private DataTable ConvertToDataTableUnitSale<ZD_PRC_UNITSALEResult>(List<ZD_PRC_UNITSALEResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_UNITSALEResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_UNITSALEResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
    private DataTable ConvertToDataTable<ZD_PRC_DOANHTHUTHEOKHUVUCResult>(List<ZD_PRC_DOANHTHUTHEOKHUVUCResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_DOANHTHUTHEOKHUVUCResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_DOANHTHUTHEOKHUVUCResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
    private DataTable ConvertToDataTableGV<ZD_PRC_GIAVONHANGBANResult>(List<ZD_PRC_GIAVONHANGBANResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_GIAVONHANGBANResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_GIAVONHANGBANResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
    private DataTable ConvertToDataTableCP<ZD_PRC_CHIPHIResult>(List<ZD_PRC_CHIPHIResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_CHIPHIResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_CHIPHIResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    protected void btnXuat_Click(object sender, EventArgs e)
    {
        if (ddlNam.SelectedValue.Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn năm!";
            return;
        }

        if (ddlPhapNhan.SelectedValue.Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn pháp nhân!";
            return;
        }
        OutReport outRPT = new OutReport();
        //lay database
        ReportBO rpt = new ReportBO();

        List<ZD_PRC_UNITSALEResult> resultUnitSale = new List<ZD_PRC_UNITSALEResult>();
        resultUnitSale = rpt.Report_UnitSale(ddlNam.SelectedValue, ddlPhapNhan.SelectedValue);
        DataTable dt4 = ConvertToDataTableUnitSale(resultUnitSale);

        List<ZD_PRC_DOANHTHUTHEOKHUVUCResult> result = new List<ZD_PRC_DOANHTHUTHEOKHUVUCResult>();
        result = rpt.Report_DoanhThu_Theo_KhuVuc(ddlNam.SelectedValue,ddlPhapNhan.SelectedValue);
        DataTable dt1 = ConvertToDataTable(result);

        List<ZD_PRC_GIAVONHANGBANResult> resultGV = new List<ZD_PRC_GIAVONHANGBANResult>();
        resultGV = rpt.Report_GiaVon(ddlNam.SelectedValue, ddlPhapNhan.SelectedValue);
        DataTable dt5 = ConvertToDataTableGV(resultGV);

        List<ZD_PRC_CHIPHIResult> resultCP = new List<ZD_PRC_CHIPHIResult>();
        resultCP = rpt.Report_ChiPhi(ddlNam.SelectedValue, ddlPhapNhan.SelectedValue);
        DataTable dt6 = ConvertToDataTableCP(resultCP);

        DataTable dt2 = new DataTable();
        DataColumn column1 = new DataColumn();
        column1.DataType = System.Type.GetType("System.String");
        column1.ColumnName = "Nam";
        dt2.Columns.Add(column1);

        DataRow row = dt2.NewRow();
        row["Nam"] = ddlNam.SelectedValue;
        dt2.Rows.Add(row);

        DataTable dt3 = new DataTable();
        DataColumn column3 = new DataColumn();
        column3.DataType = System.Type.GetType("System.String");
        column3.ColumnName = "PhapNhan";
        dt3.Columns.Add(column3);

        DataRow row1 = dt3.NewRow();
        row1["PhapNhan"] = ddlPhapNhan.SelectedItem;
        dt3.Rows.Add(row1);
        // datatable 1
        DataTable Report1 = new DataTable();
        // datatable 2
        DataTable Report2 = new DataTable();
        // datatable 3
        DataTable Report3 = new DataTable();
        // datatable 4
        DataTable Report4 = new DataTable();
        // datatable 5
        DataTable Report5 = new DataTable();
        // datatable 6
        DataTable Report6 = new DataTable();
        Report1 = dt1;
        Report2 = dt2;
        Report3 = dt3;
        Report4 = dt4;
        Report5 = dt5;
        Report6 = dt6;
        Report1.TableName = "Revenue";
        Report2.TableName = "Date";
        Report3.TableName = "Master";
        Report4.TableName = "UnitSale";
        Report5.TableName = "COGS";
        Report6.TableName = "OPEXANALYSIS";
        DataTable[] arrTable = { Report1, Report2, Report3, Report4, Report5, Report6 };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/RPT_P_L_MANAGEMENT.xls"), arrTable, "_P&L_Management.xls");

    }


}
